package com.generator.statics.inboundStatics.controller;


import com.generator.statics.inboundStatics.pojo.*;
import com.generator.statics.inboundStatics.service.IInboundStaticsService;
import com.generator.util.DateFormatUtil;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/inboundStatics")
public class InboundStaticsController {

    @Autowired
    private IInboundStaticsService joinCountService;

    public IInboundStaticsService getJoinCountService() {
        return joinCountService;
    }

    public void setJoinCountService(IInboundStaticsService joinCountService) {
        this.joinCountService = joinCountService;
    }


//    @RequestMapping("/test")
//    @ResponseBody
//    public List<Warehouse> test(){
//        List<Warehouse> warehouse = joinCountService.getWarehouse(new Warehouse());
//        return warehouse;
//    }


    //查询所有仓库的入库信息
    @RequestMapping( "/getAllRk")
    @ResponseBody
    public PageModel<QueryWarehouseList> queryAllRk(QueryWarehouseList warehouseList, PageModel<QueryWarehouseList> pageModel,HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        if (pageNum!=null){
            int i = Integer.parseInt(pageNum);
            pageModel.setPageNum(i);
        }
        PageModel<QueryWarehouseList> queryWarehouseListPageModel = joinCountService.QueryWarehouseList(warehouseList, pageModel);
        if (queryWarehouseListPageModel!=null&&!queryWarehouseListPageModel.getData().isEmpty()){
            System.out.println(queryWarehouseListPageModel);
            return queryWarehouseListPageModel;
        }
        return null;
    }


    //通过仓库名称，负责人名字获取采购单信息
    @ResponseBody
    @RequestMapping("/JoinCountDetail")
    public PageModel<JoinCountDetail> JoinCountDetail(JoinCountDetail warehouse, PageModel<JoinCountDetail> pageModel, HttpServletRequest req){
        warehouse.setWarehousename((String)req.getSession().getAttribute("warehousename"));
        warehouse.setUname((String)req.getAttribute("uname"));
        String pageNum = req.getParameter("pageNum");
        if (pageNum!=null){
            int i = Integer.parseInt(pageNum);
            pageModel.setPageNum(i);
        }
        PageModel<JoinCountDetail> joinPageModel = joinCountService.queryRkByWareID(warehouse, pageModel);
        if (joinPageModel.getData()!=null&&!joinPageModel.getData().isEmpty()){
            return joinPageModel;
        }
        return null;
    }

    //跳转stockView并把仓库名传入session
    @RequestMapping("/toStockView")
    @ResponseBody
    public void toStockView(HttpServletRequest request, HttpSession session){
        String warehousename = request.getParameter("warehousename");
        session.setAttribute("warehousename",warehousename);

//        String uname = request.getParameter("uname");
//        session.setAttribute("uname",uname);
    }

    //跳转采购详情并将采购单id存入session
    @RequestMapping("/toCPurchase")
    @ResponseBody
    public void toCPurchase(HttpServletRequest request, HttpSession session){
        String purchaseid = request.getParameter("purchaseid");
        request.getSession().setAttribute("purchaseid",purchaseid);

    }

    //跳转采购详情页
    @RequestMapping("/getCDetailsByPurchaseId")
    @ResponseBody
    public CPurchaseDetail getCDetailsByPurchaseId(HttpServletRequest request) {
        String purchaseid1 = (String)request.getSession().getAttribute("purchaseid");
        return joinCountService.getCDetailsByPurchaseId(purchaseid1);
    }

    //采购详情页拿采购明细
    @RequestMapping("/getGoodsDetail")
    @ResponseBody
    public List<CDetails> getGoodsByPurchaseId(HttpServletRequest request){
        String purchaseid = (String) request.getSession().getAttribute("purchaseid");
        return joinCountService.getGoodsByPurchaseId(purchaseid);
    }

    //获取省份
    @ResponseBody
    @RequestMapping("/getProvince")
    public List<Province> getProvince(HttpServletRequest request){
        return joinCountService.getAllProvince();
    }

    //根据省份筛选城市
    @ResponseBody
    @RequestMapping("/getCity")
    public List<City> getCity(HttpServletRequest request){
        String pname = request.getParameter("pname");
        City city = new City();
        city.setPname(pname);
        return joinCountService.getAllCity(city);
    }

    //根据条件查询入库记录
    @RequestMapping("/queryWareHouseByCondition")
    @ResponseBody
    public PageModel<QueryWarehouseList> queryWareHouseByCondition(WareHouseCondition wareHouseCondition,PageModel<QueryWarehouseList> pageModel,HttpServletRequest request){
        String  pname = request.getParameter("pname");
        String  city = request.getParameter("city");
        String  warehouname = request.getParameter("warehouname");
        String  startTime = request.getParameter("startTime");
        String  endTime = request.getParameter("endTime");
        Date startdate = DateFormatUtil.stringParseToDate(startTime);
        Date enddate = DateFormatUtil.stringParseToDate(endTime);

        String pageNum = request.getParameter("pageNum");
        if (pageNum!=null){
            int i = Integer.parseInt(pageNum);
            pageModel.setPageNum(i);
        }
        wareHouseCondition.setCity(city);
        wareHouseCondition.setPname(pname);
        wareHouseCondition.setWarehouname(warehouname);
        wareHouseCondition.setStartTime(startdate);
        wareHouseCondition.setEndTime(enddate);

        PageModel<QueryWarehouseList> wlm = joinCountService.queryWareHouseByCondition(wareHouseCondition, pageModel);
        if (wlm!=null&&!wlm.getData().isEmpty()){
            System.out.println(wlm);
            return pageModel;
        }
        return null;
    }
}
